package com.example.demo.controller;

import com.example.demo.requests.ModifyDrinkRequest;
import com.example.demo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
//    新增学生信息
    @PostMapping("/stu/add")
    public Student addNewStudent(@RequestBody Student student){
        log.info("新增学生信息：{}",student);
        Student student1=Student.builder()
                .id(1)
                .name("敖炼")
                .adminDate(LocalDate.of(2022,5,22))
                .major("计算机").build();
        return student1;
    }

//    删除学生信息
    @DeleteMapping("/stu/del/{id}")
    public String deleteStudent(@PathVariable Long id) {
        log.info("要删除的学生id: {}",id);
        return "删除学生信息成功";
    }

//    查询所有学生信息
    @GetMapping("/stu/all")
    public List<Student> getAllStudent(){
        ArrayList<Student> result=new ArrayList<>();
        result.add(Student.builder().id(1)
                .name("敖炼")
                .adminDate(LocalDate.of(2012,2,23))
                .major("数媒")
                .build());
        return result;
    }

    //查询单个学生信息，据id号
    @GetMapping("/stu/{id}")
    public Student getStudent(@PathVariable("id") Integer id){
        log.info("要查询的学生id：{}",id);
        Student student=Student.builder().id(1)
                .name("敖炼")
                .adminDate(LocalDate.of(2011,2,22))
                .major("软件技术")
                .build();
        log.info("要查询的学生信息：{}",student);
        return student;
    }

//    修改学生信息
    @PutMapping("/stu/update")
    public Student updateStudent(@RequestBody ModifyStudentRequest request) {
        log.info("要修改的学生信息：{}",request);
        Student student2=Student.builder().id(1)
                .name("敖炼")
                .adminDate(LocalDate.of(2022,2,22))
                .major("通信工程")
                .build();
        return student2;
    }

    @PostMapping(value = "/stu/uploadFile",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> uploadFile(MultipartFile file){
        List<Student> results=new ArrayList<>();
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line="";
            while ((line=reader.readLine())!=null){
                String[] args=line.split(",");
                results.add(Student.builder().id(Integer.parseInt(args[0]))
                        .name(args[1])
                        .adminDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return results;

    }

    private class ModifyStudentRequest {
    }
}
